package org.shangrila.bme.mb;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;

import org.shangrila.app.sys.cfg.AppInitInfo;
import org.shangrila.bme.eo.BmeContract;
import org.shangrila.bme.eo.BmeRtnInfo;
import org.shangrila.comm.dao.DbAccess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named
@SessionScoped
public class RtnInfoQueryMB implements Serializable{

	/** serialVersionUID*/
	private static final long serialVersionUID = -4930189961084850931L;
	
	private final Logger log=LoggerFactory.getLogger(this.getClass());
	
	private BmeRtnInfo io=new BmeRtnInfo();
	private List<BmeRtnInfo> vor=new ArrayList<>();
	
	private List<SelectItem> itemsRtnStatus=AppInitInfo.getDropdownList("RtnInfoStatus");
	
	@Inject
	private DbAccess db;

	public BmeRtnInfo getIo() {
		return io;
	}

	public void setIo(BmeRtnInfo io) {
		this.io = io;
	}

	public List<BmeRtnInfo> getVor() {
		return vor;
	}

	public void setVor(List<BmeRtnInfo> vor) {
		this.vor = vor;
	}

	public List<SelectItem> getItemsRtnStatus() {
		return itemsRtnStatus;
	}

	public void setItemsRtnStatus(List<SelectItem> itemsRtnStatus) {
		this.itemsRtnStatus = itemsRtnStatus;
	}
	
	@PostConstruct
	public void init() {
		io.setBmeContract(new BmeContract());
	}
	
	/**  
	 * <p>Title: queryAction</p>  
	 * <p>Description: 根据输入框的内容（与）查询回款单信息</p>  
	 * @return  
	 */
	public String queryAction() {
		
		StringBuilder sql = new StringBuilder("select br from BmeRtnInfo br ");
		StringBuilder where = new StringBuilder("where ");
		List<Object> params=new ArrayList<>();
		int i=0;
		if(null!=io.getRtnId()&&!"".equals(io.getRtnId())) {
			if(i++==0) {
				where.append("br.rtnId=?"+i+" ");
			}else {
				where.append("and br.rtnId=?"+i+" ");
			}
			params.add(io.getId());
		}
		if(null!=io.getRtnDate()&&!(io.getRtnDate().toString().equals(""))) {
			if(i++==0) {
				where.append("br.rtnDate=?"+i+" ");
			}else {
				where.append("and br.rtnDate=?"+i+" ");
			}
			params.add(io.getRtnDate());
		}
		if(null!=io.getBmeContract().getId()&&!"".equals(io.getBmeContract().getId())) {
			if(i++==0) {
				where.append("br.bmeContract=?"+i+" ");
			}else {
				where.append("and br.bmeContract=?"+i+" ");
			}
			params.add(io.getBmeContract());
		}
		if(null!=io.getContName()&&!"".equals(io.getContName())) {
			if(i++==0) {
				where.append("br.contName=?"+i+" ");
			}else {
				where.append("and br.contName=?"+i+" ");
			}
			
			params.add(io.getContName());
		}
		if(null!=io.getStatus()&&!"".equals(io.getStatus())) {
			if(i++==0) {
				where.append("br.status=?"+i+" ");
			}else {
				where.append("and br.status=?"+i+" ");
			}
			
			params.add(io.getStatus());
		}
		if(where.length()==6) {
			where=new StringBuilder("");
		}
		
		log.info("invInfo queryAction:"+sql.append(where.toString()));
		if("".equals(where.toString())) {
			vor=db.findBySql(sql.toString());
		}else {
			vor=db.findBySql(sql.toString(), params.toArray());
		}
		
		return null;	
	}
	
	

}
